/**
 * 
 * 文件名：LogUtils.java
 *
 * 功能：提供日志输入常用的方法
 *
 *
 * 变更记录：
 *
 * 创建者：陈保毕
 * 
 * 日期：2016年3月22日
 */
package com.hpjz.common.log;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class LogUtils {
      private LogUtils(){
    	  
      }
      
      /**
       * 默认调用深度
       */
      private static final int INER_STACKTRACE_DEEP = 2;
      private static final int BEGIN_STATE = 0;
      private static final int SUCCESS_STATE = 1;
      private static final int ERROR_STATE = -1;
      
      /**
       * 打印debug级别的日志
       * @param message
       * @param params
       */
      public static void debug(String message,Object...params){
    	  Log logger = getLogger();
    	  if (logger.isDebugEnabled()) {
			if (params.length>0) {
				message = MsgFormatter.arrayFormat(message,params);
			}
			logger.debug(message);
		}
      }
        /**
         * 根据名称打印debug日志
         * @param name
         * @param message
         * @param params
         */
      public static void debugByName(String name,String message,Object...params){
    	  Log  logger = getLogger(name);
    	  if (logger.isDebugEnabled()) {
			if (params.length>0) {
				message = MsgFormatter.arrayFormat(message, params);
			}
			logger.debug(message);
		}
    	  
      }
      
       /**
        * 
        * @param message
        * @param t
        * @param params
        */
      public static void debug(String message,Throwable t,Object ... params){
    	  Log logger = getLogger();
    	  if (logger.isDebugEnabled()) {
			if (params.length>0) {
				message = MsgFormatter.arrayFormat(message, params);
			}
			logger.debug(message, t);
		}
      }
      
      /**
       * 根据名称打印debug日志
       * @param name
       * @param message
       * @param t
       * @param params
       */
      public static void debugByName(String name,String message,Throwable t,Object ... params){
    	  Log logger = getLogger(name);
    	  if (logger.isDebugEnabled()) {
			if (params.length>0) {
				message = MsgFormatter.arrayFormat(message, params);
			}
			logger.debug(message, t);
		}
      }
      
      public static void debug(Object message){
    	  Log logger = getLogger();
    	  if (logger.isDebugEnabled()) {
			logger.debug(message);
		}
      }
      
      
      public static void debug(Object message,Throwable t){
    	  Log logger = getLogger();
    	  if (logger.isDebugEnabled()) {
			logger.debug(message,t);
		}
      }
      
      
      /**
       * info 级别信息日志
       * @param message
       * @param params
       */
      public static void info(String message,Object...params){
    	  Log logger = getLogger();
    	  if (logger.isInfoEnabled()) {
			if (params.length>0) {
				message = MsgFormatter.arrayFormat(message, params);
			}
		}
    	  logger.info(message);
      }
      
      
      public static void infoByName(String name,String message,Object...params){
    	  Log logger = getLogger(name);
    	  if (logger.isInfoEnabled()) {
			if (params.length>0) {
				message = MsgFormatter.arrayFormat(message, params);
			}
		}
    	  logger.info(message);
      }
      
      
      public static void info(String message,Throwable t, Object...params){
    	  Log logger = getLogger();
    	  if (logger.isInfoEnabled()) {
			if (params.length>0) {
				message = MsgFormatter.arrayFormat(message, params);
			}
		}
    	  logger.info(message,t);
      }
      
      
      public static void infoByName(String name,String message,Throwable t, Object...params){
    	  Log logger = getLogger(name);
    	  if (logger.isInfoEnabled()) {
			if (params.length>0) {
				message = MsgFormatter.arrayFormat(message, params);
			}
		}
    	  logger.info(message,t);
      }
      
      
      public static void info(Object message){
    	  Log logger = getLogger();
    	  if (logger.isInfoEnabled()) {
			logger.info(message);
		}
      }
      
      
      public static void info(Object message,Throwable t){
    	  Log logger = getLogger();
    	  if (logger.isInfoEnabled()) {
			logger.info(message,t);
		}
      }
      

      public static void warn(String message,Object...params){
    	  Log logger = getLogger();
    	  if (logger.isWarnEnabled()) {
			if (params.length>0) {
				message = MsgFormatter.arrayFormat(message, params);
			}
			logger.info(message);
		}
      }
      
      
      public static void warnByName(String name, String message,Object...params){
    	  Log logger = getLogger(name);
    	  if (logger.isWarnEnabled()) {
			if (params.length>0) {
				message = MsgFormatter.arrayFormat(message, params);
			}
			logger.info(message);
		}
      }
      
      
      public static void warn(String message,Throwable t,Object...params){
    	  Log logger = getLogger();
    	  if (logger.isWarnEnabled()) {
			if (params.length>0) {
				message = MsgFormatter.arrayFormat(message, params);
			}
			logger.info(message,t);
		}
      }
      
      
      public static void warn(String name,String message,Throwable t,Object...params){
    	  Log logger = getLogger(name);
    	  if (logger.isWarnEnabled()) {
			if (params.length>0) {
				message = MsgFormatter.arrayFormat(message, params);
			}
			logger.info(message,t);
		}
      }
      
      public static void warn(Object message){
    	  Log logger = getLogger();
    	  if (logger.isWarnEnabled()) {
			logger.warn(message);
		}
      }
      
      public static void warn(Object message,Throwable t){
    	  Log logger = getLogger();
    	  if (logger.isWarnEnabled()) {
			logger.warn(message,t);
		}
      }
      
      
      /**
       * error级别的日志
       * @param message
       * @param params
       */
      
      public static void error(String message,Object...params){
    	/*  Log logger = getLogger();
    	  if (logger.isInfoEnabled()) {
			if (params.length>0) {
				message = MsgFormatter.arrayFormat(message, params);
			}
		}
    	  logger.info(message);*/
    	  
    	  
    	  Log logger = getLogger();
    	  if (logger.isErrorEnabled()) {
			if (params.length>0) {
				message = MsgFormatter.arrayFormat(message, params);
			}
			logger.error(message);
		}
      }
      
      
      public static void error(String message,Throwable t,Object...params){
    	  Log logger = getLogger();
    	  if (logger.isErrorEnabled()) {
			if (params.length>0) {
				message = MsgFormatter.arrayFormat(message, params);
			}
			logger.error(message,t);
		}
      }
      
      
      public static void errorByName(String name,String message,Throwable t,Object...params){
    	  Log logger = getLogger(name);
    	  if (logger.isErrorEnabled()) {
			if (params.length>0) {
				message = MsgFormatter.arrayFormat(message, params);
			}
			logger.error(message,t);
		}
      }
      
      public static void error(Object message){
    	  Log logger = getLogger();
    	  if (logger.isErrorEnabled()) {
			logger.error(message);
		}
      }
      
      public static void error(Object message,Throwable t){
    	  Log logger = getLogger();
    	  if (logger.isErrorEnabled()) {
			logger.error(message,t);
		}
      }
      
      
     /**
      * 
      * 输出sysout打印日志
      */
    /* public static void getSysLog(String msg){
    	 System.out.println(msg);
     } */
     
     
     private static Log getLogger(){
    	 final Throwable t = new Throwable();
    	 final StackTraceElement[] methodCaller = t.getStackTrace();
    	 return LogFactory.getLog(methodCaller[INER_STACKTRACE_DEEP].getClassName());
    	 
     }
     
     
     private static Log getLogger(String name){
    	 return LogFactory.getLog(name);
     }
     
      
}
